package com.example.yiwei.crm.view.activity.JYCoordingActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.TimePickerView;
import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.data.GetManyidu;
import com.example.yiwei.crm.entity.BZFLEntity;
import com.example.yiwei.crm.entity.CustomerMessageEntity;
import com.example.yiwei.crm.entity.HospitalEntity;
import com.example.yiwei.crm.entity.ManyiduEntity;
import com.example.yiwei.crm.json.CoorCustomerJson;
import com.example.yiwei.crm.json.YYSpinnerJson;
import com.example.yiwei.crm.json.coordinationjson;
import com.example.yiwei.crm.util.NetWorkUtils;
import com.example.yiwei.crm.util.SC;
import com.example.yiwei.crm.view.Class.listviewClass.CustomerSelectAdapter;
import com.example.yiwei.crm.view.activity.jdjycooractivity.JDJYCoorActivity;
import com.example.yiwei.crm.view.activity.jdjycooractivity.JYJDAddManyiduActivity;
import com.example.yiwei.crm.view.activity.jtCustomer.JDcoorActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class JYAddJDActivity extends AppCompatActivity  {
    private TextView txt_manager,txt_name,txt_age,txt_sex, txt_ly_time,txt_time;
    private EditText edit_mxb,edit_yy,txt_ys,txt_qk,txt_phone,txt_yb;
    private Spinner sp_ly_l,sp_ly_r,sp_jz_l,sp_jz_r,sp_pj,sp_bz,sp_jz_hospital;
    private Button button1,button2;
    private TimePickerView mpv;
    private TimePickerView mpv1;
    private List<BZFLEntity> list3;
    private String tishi;
    private Date date1,date2;
    private int bz_id,pj_id;
    private String bz_s;
    private SharedPreferences sp;
    private List<String> list_pj,hospital_list;
    private String lysj,sj,ssp_ly_l,ssp_yl_r,ssp_jz_l,ssp_jzy;
    private List<String> list,bz_list,list4;
    private ManyiduEntity manyiduEntity;
    private List<ManyiduEntity> mlist;
    private ArrayAdapter adapter1,adapter2,adapter3,adapter4,adapter5,adapter6,adapter7;
    private ImageView im_back;
    private List<HospitalEntity> list1=new ArrayList<>();
    String result;
    private Button button;
    private String Ly_left,ALy_left;
    private ImageView img_search,img_luru,img_me;
    private ImageView img_select_customer;
    private PopupWindow mPopWindow;
    private EditText edit_search;
    private Button btn_search;
    private ListView listView;
    private CustomerSelectAdapter adapter;
    private List<CustomerMessageEntity> clist;
    private CustomerMessageEntity customer;
    private LinearLayout linearLayout;
    private String hospital_name;
    private int hosptial_id;
    private TextView txt_title;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jyadd_jd);
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        manyiduEntity= GetManyidu.get();
        img_luru= (ImageView) findViewById(R.id.item_image_lr);
        txt_title= (TextView) findViewById(R.id.healt_education_add_txt_title);
        img_search= (ImageView) findViewById(R.id.item_image_cx);
        img_select_customer = (ImageView) findViewById(R.id.img_xiangxia);
        linearLayout = (LinearLayout) findViewById(R.id.add_coor_linear_tj);
        img_me = (ImageView) findViewById(R.id.item_image_w);
        sp_jz_hospital = (Spinner) findViewById(R.id.myd_add_jz_hospital);

        if ("update".equals(getIntent().getStringExtra("tag"))){
            img_select_customer.setVisibility(View.GONE);
            txt_title.setText("修改满意度");
        }
        img_select_customer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SC sc = new SC();
                        JSONObject object = sc.storage(sp);
                        String json = object.toString();
                        String result = NetWorkUtils.getCount(baseClass.find_myd_all_customer+1,json);
                        Log.d("ccc",result);
                        clist = CoorCustomerJson.jsonVisitMessage(result);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (clist!=null){
                                    adapter = new CustomerSelectAdapter(JYAddJDActivity.this,clist);
                                    showPopupWindow();
                                }
                            }
                        });
                    }
                }).start();
            }
        });
        img_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("jd".equals(getIntent().getStringExtra("aaa"))){
                    Intent intent=new Intent(JYAddJDActivity.this,JDJYCoorActivity.class);
                    intent.putExtra("ii","ii");
                    startActivity(intent);
                    finish();
                }else {
                    Intent intent=new Intent(JYAddJDActivity.this,JDcoorActivity.class);
                    intent.putExtra("ii","ii");
                    startActivity(intent);
                    finish();
                }
            }
        });
        img_luru.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("jd".equals(getIntent().getStringExtra("aaa"))){
                    Intent intent=new Intent(JYAddJDActivity.this,JDJYCoorActivity.class);
                    startActivity(intent);
                    finish();
                }else {
                    Intent intent=new Intent(JYAddJDActivity.this,JDcoorActivity.class);
                    startActivity(intent);
                    finish();
                }

            }
        });
        img_me.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("jd".equals(getIntent().getStringExtra("aaa"))){
                    Intent intent=new Intent(JYAddJDActivity.this,JDJYCoorActivity.class);
                    intent.putExtra("me","me");
                    startActivity(intent);
                    finish();
                }else {
                    Intent intent=new Intent(JYAddJDActivity.this,JDcoorActivity.class);
                    intent.putExtra("me","me");
                    startActivity(intent);
                    finish();
                }
            }
        });

        list = new ArrayList<>();
        list.add("");
        list.add("2.0(5.3)");
        list.add("1.5(5.2)");
        list.add("1.2(5.1)");
        list.add("1.0(5.0)");
        list.add("0.8(4.9)");
        list.add("0.6(4.8)");
        list.add("0.5(4.7)");
        list.add("0.4(4.6)");
        list.add("0.3(4.5)");
        list.add("0.25(4.4)");
        list.add("0.2(4.3)");
        list.add("0.15(4.2)");
        list.add("0.12(4.1)");
        list.add("0.1(4.0)");
        list.add("0.08");
        list.add("0.06");
        list.add("0.04");
        list.add("0.02");
        list.add("指数");
        list.add("手动");
        list.add("光感");
        list.add("无光感");
        list_pj = new ArrayList<>();
        list_pj.add("非常满意");
        list_pj.add("满意");
        list_pj.add("一般");
        list_pj.add("不满意");
        txt_manager = (TextView) findViewById(R.id.myd_add_manger_tv);
        button = (Button) findViewById(R.id.add_jd_btn);
        im_back = (ImageView) findViewById(R.id.add_jd_img_back);
        if ("jd".equals(getIntent().getStringExtra("aaa"))){
            linearLayout.setVisibility(View.GONE);
            im_back.setVisibility(View.GONE);
        }else {
            im_back.setVisibility(View.VISIBLE);
        }
        im_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        txt_name = (TextView) findViewById(R.id.myd_add_name);
        txt_age = (TextView) findViewById(R.id.myd_add_age);
        txt_sex = (TextView) findViewById(R.id.myd_add_sex);
        txt_phone = (EditText) findViewById(R.id.myd_add_phone);
        txt_ly_time = (TextView) findViewById(R.id.myd_add_ly_time);
        txt_ys = (EditText) findViewById(R.id.myd_add_ys_tv);
        txt_qk = (EditText) findViewById(R.id.myd_add_jzqk_tv);
        txt_time = (TextView) findViewById(R.id.myd_add_time);
        edit_yy = (EditText) findViewById(R.id.myd_add_yy_edit);
        txt_yb = (EditText) findViewById(R.id.myd_add_yb_number);
        edit_mxb = (EditText) findViewById(R.id.myd_add_mxb);
        sp_ly_l = (Spinner) findViewById(R.id.myd_ly_l_sp);
        sp_ly_r = (Spinner) findViewById(R.id.myd_ly_r_sp);
        sp_jz_r = (Spinner) findViewById(R.id.myd_jz_r_sp);
        sp_jz_l = (Spinner) findViewById(R.id.myd_jz_l_sp);
        sp_pj = (Spinner) findViewById(R.id.myd_add_pj_sp);
        sp_bz = (Spinner) findViewById(R.id.myd_add_bz);
        button1 = (Button) findViewById(R.id.myd_add_btn_show1);
        button2 = (Button) findViewById(R.id.myd_add_time_btn_show1);
        //创建选择器
        mpv = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置时间范围
        Calendar calendar = Calendar.getInstance();
        mpv.setRange(calendar.get(Calendar.YEAR) - 100, calendar.get(Calendar.YEAR) + 100);
        //插入时间
        mpv.setTime(new Date());
        //选择监听
        mpv.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                date1 =date;
                lysj=getTime(date);
                txt_ly_time.setText(lysj);
            }
        });
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mpv.show();
            }
        });
        if ("".equals(txt_ly_time.getText().toString())){
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            txt_ly_time.setText(df.format(new Date()));
        }
        if ("".equals(txt_time.getText().toString())){
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            txt_time.setText(df.format(new Date()));
        }
        //创建选择器
        mpv1 = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置时间范围
        Calendar calendar1 = Calendar.getInstance();
        mpv1.setRange(calendar.get(Calendar.YEAR) - 100, calendar1.get(Calendar.YEAR) + 100);
        //插入时间
        mpv1.setTime(new Date());
        //选择监听
        mpv1.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                date2 = date;
                sj=getTime(date);
                txt_time.setText(sj);
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mpv1.show();
            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                JSONObject object = new JSONObject();
                try {
                    object.put("KYA01",getIntent().getIntExtra("KYA01",0));
                    object.put("DEL01",0);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String json = object.toString();
                if ("update".equals(getIntent().getStringExtra("tag"))){
                    manyiduEntity = coordinationjson.jsonmanyiduMessage(baseClass.find_one_myd_msg_URL,json);
                }else {
                    manyiduEntity = coordinationjson.jsonmanyiduMessage(baseClass.find_myd_msg_URL,json);
                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if ("update".equals(getIntent().getStringExtra("tag"))){
                            setMessage();
                        }
//                        initData();
                    }
                });
            }
        }).start();
        adapter1 = new ArrayAdapter(JYAddJDActivity.this, R.layout.spinner_display_style, R.id.txtvwSpinner, list);
        adapter1.setDropDownViewResource(R.layout.spinner_dropdown_style);
        sp_ly_l.setAdapter(adapter1);
        sp_ly_l.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (position!=0){
                    ssp_ly_l= list.get(position);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        adapter2 = new ArrayAdapter(JYAddJDActivity.this, R.layout.spinner_display_style, R.id.txtvwSpinner, list);
        adapter2.setDropDownViewResource(R.layout.spinner_dropdown_style);
        sp_ly_r.setAdapter(adapter2);
        sp_ly_r.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (position!=0){
                    ssp_yl_r= list.get(position);
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        adapter3 = new ArrayAdapter(JYAddJDActivity.this, R.layout.spinner_display_style, R.id.txtvwSpinner, list);
        adapter3.setDropDownViewResource(R.layout.spinner_dropdown_style);
        sp_jz_l.setAdapter(adapter3);
        sp_jz_l.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                if (position!=0){
                    ssp_jz_l= list.get(position);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        adapter4 = new ArrayAdapter(JYAddJDActivity.this, R.layout.spinner_display_style, R.id.txtvwSpinner, list);
        adapter4.setDropDownViewResource(R.layout.spinner_dropdown_style);
        sp_jz_r.setAdapter(adapter4);
        sp_jz_r.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                if (position!=0){
                    ssp_jzy= list.get(position);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc1 = new SC();
                JSONObject object3 = sc1.storage(sp);
                String json2 = object3.toString();
                String result2 = NetWorkUtils.getCount(baseClass.find_BZFL_URL,json2);
                list3 = YYSpinnerJson.getBZFL(result2);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        bz_list= new ArrayList<>();
                        for (int i2=0;i2<list3.size();i2++){
                            bz_list.add(list3.get(i2).getName());
                        }
                        adapter5 = new ArrayAdapter(JYAddJDActivity.this,R.layout.spinner_display_style,R.id.txtvwSpinner,bz_list);
                        adapter5.setDropDownViewResource(R.layout.spinner_dropdown_style);
                        sp_bz.setAdapter(adapter5);
                        sp_bz.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                    bz_s=list3.get(position).getName();
                                    bz_id = list3.get(position).getId();
                            }
                            @Override
                            public void onNothingSelected(AdapterView<?> parent) {

                            }
                        });
                        if ("update".equals(getIntent().getStringExtra("tag"))){
                            manyiduEntity=GetManyidu.get();
                            for (int i=0;i<list3.size();i++){
                            if (manyiduEntity.getBZ().equals(list3.get(i).getName())){
                                sp_bz.setSelection(i);
                            }
                        }
                        }


                    }

                });
            }
        }).start();
        adapter6 = new ArrayAdapter(JYAddJDActivity.this, R.layout.spinner_display_style, R.id.txtvwSpinner, list_pj);
        adapter6.setDropDownViewResource(R.layout.spinner_dropdown_style);
        sp_pj.setAdapter(adapter6);
        sp_pj.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                pj_id= position+1;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
       button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("".equals(txt_ly_time.getText().toString())&&"".equals(txt_time.getText().toString())){
                    Toast.makeText(JYAddJDActivity.this, "请选择时间", Toast.LENGTH_SHORT).show();
                }else if (!"update".equals(getIntent().getStringExtra("tag"))&&customer==null){
                    Toast.makeText(JYAddJDActivity.this, "请选择患者", Toast.LENGTH_SHORT).show();
                }else {
                    add();
                }
            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc = new SC();
                JSONObject object = sc.storage(sp);
                try {
                    object.put("UAA04",1);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String json = object.toString();
                String result = NetWorkUtils.getCount(baseClass.find_hospital_add_yy_URL, json);
                Log.d("ccc",result);
                list1= YYSpinnerJson.getHospital(result);
                Log.d("aaa",list.size()+"");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        hospital_list= new ArrayList<>();
                        if (list1!=null){
                            for (int i2=0;i2<list1.size();i2++){
                                hospital_list.add( list1.get(i2).getName());
                            }
                        }

                        adapter7 = new ArrayAdapter(JYAddJDActivity.this,R.layout.spinner_display_style,R.id.txtvwSpinner, hospital_list);
                        adapter7.setDropDownViewResource(R.layout.spinner_dropdown_style);
                        sp_jz_hospital.setAdapter(adapter7);
                        sp_jz_hospital.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                hospital_name = hospital_list.get(position);
                                hosptial_id =list1.get(position).getId();
                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> parent) {

                            }
                        });
                        if ("update".equals(getIntent().getStringExtra("tag"))){
                            manyiduEntity=GetManyidu.get();
                            if (list1!=null&&manyiduEntity.getHospital()!=null){
                                for (int i=0;i<list1.size();i++){
                                    if (manyiduEntity.getHospital().equals(list1.get(i).getName())){
                                        sp_jz_hospital.setSelection(i);
                                    }
                                }
                            }
                        }
                    }
                });
            }
        }).start();
    }
    public void setMessage(){
        manyiduEntity=GetManyidu.get();
//        txt_time.setText(manyiduEntity.getDate());
        txt_ys.setText(manyiduEntity.getDoctor_name());
        txt_qk.setText(manyiduEntity.getState());
        txt_ly_time.setText(manyiduEntity.getHospital_time());
        Ly_left=manyiduEntity.getLy_left();
        ALy_left=manyiduEntity.getAly_left();
        if (Ly_left!=null){
            if (Ly_left.equals(ALy_left)){
                for (int i= 0;i<list.size();i++){
                    if (manyiduEntity.getLy_left().equals(list.get(i))){
                        sp_ly_l.setSelection(i);
                    }
                }
            }else {
                for (int i= 0;i<list.size();i++){
                    if ((manyiduEntity.getLy_left()+"("+manyiduEntity.getAly_left()+")").equals(list.get(i))){
                        sp_ly_l.setSelection(i);
                    }
                }
            }
        }
        if (manyiduEntity.getLy_right()!=null){
            if (manyiduEntity.getLy_right().equals(manyiduEntity.getAly_right())){
                for (int i= 0;i<list.size();i++){
                    if (manyiduEntity.getLy_right().equals(list.get(i))){
                        sp_ly_r.setSelection(i);
                    }
                }
            }else {
                for (int i= 0;i<list.size();i++){
                    if ((manyiduEntity.getLy_right()+"("+manyiduEntity.getAly_right()+")").equals(list.get(i))){
                        sp_ly_r.setSelection(i);
                    }
                }
            }
        }
        if (manyiduEntity.getJz_left()!=null){
            if (manyiduEntity.getJz_left().equals(manyiduEntity.getAjz_left())){
                for (int i= 0;i<list.size();i++){
                    if (manyiduEntity.getJz_left().equals(list.get(i))){
                        sp_jz_l.setSelection(i);
                    }
                }
            }else {
                for (int i= 0;i<list.size();i++){
                    if ((manyiduEntity.getJz_left()+"("+manyiduEntity.getAjz_left()+")").equals(list.get(i))){
                        sp_jz_l.setSelection(i);
                    }
                }
            }
        }
        if (manyiduEntity.getJz_right()!=null){
            if (manyiduEntity.getJz_right().equals(manyiduEntity.getAjz_right())){
                for (int i= 0;i<list.size();i++){
                    if (manyiduEntity.getAjz_left().equals(list.get(i))){
                        sp_jz_r.setSelection(i);
                    }
                }
            }else{
                for (int i=0;i<list.size();i++){
                    if ((manyiduEntity.getJz_right()+"("+manyiduEntity.getAjz_right()+")").equals(list.get(i))){
                        sp_jz_r.setSelection(i);
                    }
                }
            }
        }
        if (manyiduEntity.getManyidu()!=0){
            sp_pj.setSelection(manyiduEntity.getManyidu()-1);
        }
        edit_mxb.setText(manyiduEntity.getMxb());
        edit_yy.setText(manyiduEntity.getResult());
        txt_name.setText(manyiduEntity.getName());
        txt_manager.setText(manyiduEntity.getManager_name());
        txt_age.setText(manyiduEntity.getAge()+"");
        if (manyiduEntity.getSex()==1){
            txt_sex.setText("男");
        }else {
            txt_sex.setText("女");
        }
        txt_phone.setText(manyiduEntity.getPhone());
        txt_yb.setText(manyiduEntity.getYb());
        txt_time.setText(manyiduEntity.getAdd_manyidu_time());

    }
    public void initData(){
        txt_manager.setText(customer.getClient_manager_name());
        txt_name.setText(customer.getClient_name());
        txt_age.setText(customer.getClient_age()+"");
        if (customer.getClient_gender()==1){
            txt_sex.setText("男");
        }else {
            txt_sex.setText("女");
        }
        txt_phone.setText(customer.getContact_phone_number());
        txt_yb.setText(customer.getSecurity_numbers());

    }
    public static String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }
    public void add(){
        String a = getTime(new Date());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟
        String dstr=txt_time.getText().toString();
        try {
            date1=sdf1.parse(dstr);
            date2 =  formatter.parse(a);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (date1 != null && date2 != null) {
            if (!date2.before(date1)) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SC sc = new SC();
                        JSONObject object = sc.storage(sp);
                        try {
                            if ("update".equals(getIntent().getStringExtra("tag"))) {

                            } else {
                                object.put("KAA01", customer.getClient_id());
                                object.put("CID03", sp.getString("CID03", null));
                            }
                            object.put("KMA15", ssp_ly_l);
                            object.put("KMA16", ssp_yl_r);
                            object.put("KMA17", ssp_jz_l);
                            object.put("KMA18", ssp_jzy);
                            object.put("KMA06", edit_mxb.getText().toString());
                            object.put("AKB01", bz_id);
                            if ("update".equals(getIntent().getStringExtra("tag"))) {

                            } else {
                                if ("jd".equals(getIntent().getStringExtra("aaa"))) {
                                    object.put("KMA19", 0);
                                } else {
                                    object.put("KMA19", 1);
                                }
                            }

                            if ("update".equals(getIntent().getStringExtra("tag"))) {
                                object.put("addoredit", "edit");
                                object.put("KMA01", manyiduEntity.getMyd_id());
                                Log.d("aaaaa", manyiduEntity.getMyd_id() + "");
                            } else {
                                object.put("addoredit", "add");
                            }
                            object.put("KMA21", txt_ys.getText().toString());
                            object.put("KMA22", txt_qk.getText().toString());
                            object.put("KMA08", edit_yy.getText().toString());
                            object.put("KMA09", pj_id);
                            object.put("KMA20", txt_ly_time.getText().toString());
                            object.put("KMA10", txt_time.getText().toString());
                            object.put("KMA11", sp.getString("CID01", sp.getString("CID01", null)));
                            object.put("KMA12", sp.getString("CID03", sp.getString("CID03", null)));
                            object.put("KMA13", sp.getString("UNT02", sp.getString("UNT02", null)));
                            object.put("KMA14", sp.getString("UNT03", sp.getString("UNT03", null)));
                            object.put("KMA24", hosptial_id);
                            object.put("KMA23", txt_manager.getText().toString());
                            object.put("KMA26", txt_phone.getText().toString());
                            object.put("KMA25", txt_yb.getText().toString());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        String json = object.toString();
                        result = NetWorkUtils.getCount(baseClass.add_myd_URL, json);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    JSONObject object1 = new JSONObject(result);
                                    String b = object1.getString("ifsuccess");
                                    if ("true".equals(b)) {
                                        if ("jd".equals(getIntent().getStringExtra("aaa"))) {
                                            Intent intent = new Intent(JYAddJDActivity.this, JDJYCoorActivity.class);
                                            intent.putExtra("ii", "ii");
                                            startActivity(intent);
                                            finish();
                                        } else {
                                            finish();
                                        }
                                        Toast.makeText(JYAddJDActivity.this, "操作成功", Toast.LENGTH_SHORT).show();
                                    } else{
                                        tishi = object1.getString("tishi");
                                        if ("update".equals(getIntent().getStringExtra("tag"))) {
                                            Toast.makeText(JYAddJDActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
                                        } else {
                                            if ("".equals(tishi)) {
                                                Toast.makeText(JYAddJDActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
                                            } else {
                                                Toast.makeText(JYAddJDActivity.this, tishi, Toast.LENGTH_SHORT).show();
                                            }
                                        }
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                    }
                }).start();
            }
            else{
                Toast.makeText(this, "填写满意度日期必须早于当前日期", Toast.LENGTH_SHORT).show();
            }
        }
    }
    private void showPopupWindow() {
        View contentView = LayoutInflater.from(JYAddJDActivity.this).inflate(
                R.layout.popuwindow_select_customer, null);
        mPopWindow = new PopupWindow(contentView);
        mPopWindow.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        mPopWindow.setHeight( LinearLayout.LayoutParams.WRAP_CONTENT);

        contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int popupWidth = contentView.getMeasuredWidth();    //  获取测量后的宽度
        final int popupHeight = contentView.getMeasuredHeight();  //获取测量后的高度
        int[] location = new int[2];
        edit_search = (EditText) contentView.findViewById(R.id.edit_seacher);
        btn_search = (Button) contentView.findViewById(R.id.btn_show);
        listView = (ListView) contentView.findViewById(R.id.btn_listview);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                customer = clist.get(position);
                initData();
                mPopWindow.dismiss();
            }
        });
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SC sc = new SC();
                        JSONObject object = sc.storage(sp);
                        try {
                            object.put("KAA02",edit_search.getText().toString());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        String json = object.toString();
                        String result = NetWorkUtils.getCount(baseClass.find_myd_all_customer+1,json);
                        clist = CoorCustomerJson.jsonVisitMessage(result);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (clist==null){
                                    Toast.makeText(JYAddJDActivity.this, "搜索不到客户！！", Toast.LENGTH_SHORT).show();
                                }else {
                                    adapter = new CustomerSelectAdapter(JYAddJDActivity.this,clist);
                                    listView.setAdapter(adapter);
                                }
                                adapter.notifyDataSetChanged();
                            }
                        });
                    }
                }).start();
            }
        });
        img_select_customer.getLocationOnScreen(location);
        mPopWindow.setAnimationStyle(R.style.contextMenuAnim);
        mPopWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.pop_bg));
        mPopWindow.setTouchable(true);
        mPopWindow.setOutsideTouchable(true);   //设置外部点击关闭ppw窗口
        mPopWindow.setFocusable(true);
        mPopWindow.update();
        mPopWindow.showAsDropDown(img_select_customer);
        backgroundAlpha(0.5f);
        mPopWindow.setOnDismissListener(new poponDismissListener());
    }
    public void backgroundAlpha(float bgAlpha)
    {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha= bgAlpha;
        getWindow().setAttributes(lp);
    }
    class poponDismissListener implements PopupWindow.OnDismissListener{

        @Override
        public void onDismiss() {
            // T
            // ODO Auto-generated method stub
            //Log.v("List_noteTypeActivity:", "我是关闭事件");
            backgroundAlpha(1f);
        }

    }

}
